package com.pn.service.impl;

import com.pn.entity.RoleAuth;
import com.pn.entity.dto.AssignAuthDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import com.pn.mapper.RoleAuthMapper;
import com.pn.service.RoleAuthService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RoleAuthServiceImpl implements RoleAuthService {

    @Resource
    private RoleAuthMapper roleAuthMapper;

    @Override
    public List<Integer> authIdsByRid(Integer roleId) {
        return roleAuthMapper.findAuthIdsByRid(roleId);
    }

    @Transactional
    @Override
    public void assignAuth(AssignAuthDto assignAuthDto) {

        // 根据roleId删除之前添加的角色
        roleAuthMapper.deleteRoleAuthByRid(assignAuthDto.getRoleId());

        List<Integer> authIds = assignAuthDto.getAuthIds();

        // 遍历权限集合
        for (Integer authId : authIds) {

            RoleAuth roleAuth = new RoleAuth();
            roleAuth.setRoleId(assignAuthDto.getRoleId());
            roleAuth.setAuthId(authId);

            // 入库
            roleAuthMapper.insertRoleAuth(roleAuth);
        }
    }
}
